// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from '../../store'
import '../../styles/common.css'

Vue.config.productionTip = false

Vue.axios.interceptors.request.use(
	function (config) {
		if (store.state.user.token) {
			config.headers.Authorization = `${store.state.user.token}`;
		}
		return config;
	},
	function (error) {
		// Do something with request error
		return Promise.reject(error);
	}
);

// 添加一个响应拦截器
Vue.axios.interceptors.response.use(function (response) {
		if(response.data.code=='1001'||response.data.code=='1002'){//具体的判断token失效的参数
			store.dispatch('deleteToken')
			store.dispatch('deleteUser')
			//sessionStorage.setItem("LoginUser",'{}')
			alert(response.data.msg);
			location.assign('/login/login.html')
		}else{
			return response
		}
	},
	function (error) {
		if (error.response) {
			switch (error.response.status) {
				case 401:
					store.dispatch('deleteToken')
					store.dispatch('deleteUser')
					location.assign('/login/login.html')
			}
		}
		return Promise.reject(error);
	}
);

new Vue({
	el: '#app',
	router,
	store,
	template: '<App/>',
	components: { App }
})
